<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>系统文章列表</title>
  <link rel="stylesheet" href="../../inc/layui/css/layui.css">
  <link rel="stylesheet" href="../../css/comm.css">
</head>

<body>
  <div class="layui-form" id="search-form">

  </div>
  <table class="layui-hide" id="table" lay-filter="table"></table>
</body>
<script type="text/javascript" src="../../inc/layui/layui.js"></script>
<script type="text/javascript" src="../../inc/g.js"></script>
<script type="text/javascript" src="../../inc/area.js"></script>
<script type="text/html" id="seeDetail">
    <a class="layui-btn layui-btn-edit layui-btn-sm" lay-event="edit">修改</a>
</script>
<script type="text/html" id="seeEnable">
    {{# if(d.Enable==1){ }}
    {{# if(d.ArticleType==1){ }}
    <a class="layui-btn layui-btn-disabled layui-btn-sm" disable="disable" lay-event="updateNoEna">已启用</a>
    {{# } else { }}
    <a class="layui-btn layui-btn-enable layui-btn-sm" lay-event="updateNoEna">已启用</a>
    {{# } }}
    {{# } else { }}
    <a class="layui-btn layui-btn-disable layui-btn-sm" lay-event="updateEna">未启用</a>
    {{# } }}
</script>

<script type="text/javascript">
  function save(d) {
    if (d) {
      parent.cms.deltab("sys_update");
      var url = 'sys/article/article_detail.html?id=' + d.ArticleID
      url += "&type=update"
      parent.cms.addtab("修改", url, 'sys_update' + d.ArticleID)
    } else {
      var url = 'sys/article/article_detail.html?'
      url += "type=add"
      parent.cms.addtab("新增", url, 'sys_add')
    }
  }

  function add(d) {
    save(null);
  }

  //加载页面
  function pageload() {
    //初始化列表
    Comm.table.render({
      elem: '#table',
      url: config.root + '/admin/article/articleList', //列表请求接口
      where: { //接口参数
        kinds: 1
      },
      cols: [
        [
          {
            checkbox: true
          },
          {
            field: 'articleId',
            title: '标题'
          },
          {
            field: 'title',
            title: '标题'
          },
          {
            field: 'kinds',
            title: '类别',
            renderData: function (d) {
              return ArticleType(d.kinds)
            }
          },
          {
            field: "Section",
            title: "详情",
            width: 550,
            renderData: function (d, v) {
              if (v) {
                v = v.replace(/\s+/g, "").replace(/[ ]|&nbsp;/g, '').replace(/<\/?.+?>/g,
                  "")
                  .replace(/ /g, "");
                if (v.length > 50) {
                  return v.substring(0, 50) + "...";
                }
              }
              return v;
            }
          },
          {
            field: "SubDate",
            title: "创建时间",
            sort: true,
            renderData: function (d) {
              return d.SubDate.split(".")[0];
            }
          }, {
            field: "Enable",
            title: "是否禁用",
            toolbar: "#seeEnable",
            width: 120,
            align: "center"
          }, {
            field: "",
            title: "操作",
            toolbar: "#seeDetail",
            width: 160
          }
        ]
      ],
      limit: 15, //分页大小
      id: 'table',
      page: true, //是否分页
      height: 'full-' + ($(".layui-form").height() + 50), //计算table底部距离
    });

    //监听table列表里按钮点击事件
    Comm.table.on('tool(table)', function (obj) {
      if (obj.event == 'edit') {
        save(obj.data)
      }
      if (obj.event == 'updateEna') {
        AJAX.POST('/admin/article/enable', {
          articleId: obj.data.ArticleID
        }, function (d) {
          if (d.code == 1) {
            layer.msg("启用成功!", {
              icon: 1
            })
            //执行重载
            Comm.table.reload('table');
          } else {
            layer.msg("启用失败!", {
              icon: 5
            })
          }
        })
      } else if (obj.event == 'updateNoEna') {
        if (obj.data.ArticleType == 1) {
          layer.msg("禁止禁用!", {
            icon: 5
          })
        } else {
          AJAX.POST('/admin/article/disable', {
            articleId: obj.data.ArticleID
          }, function (d) {
            if (d.code == 1) {
              layer.msg("禁用成功!", {
                icon: 1
              })
              //执行重载
              Comm.table.reload('table');
            } else {
              layer.msg("禁用失败!", {
                icon: 5
              })
            }
          })
        }
      }
    });

    // 类别
    function ArticleType(i) {
      if (i == 1) {
        return "系统文章";
      } else if (i == 2) {
        return "帮助文档";
      } else {
        return "其他";
      }
    }
  }
</script>

</html>